/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-30
 * Time: 下午12:16
 * To change this template use File | Settings | File Templates.
 */
/*
 *  首页slide效果
 */
$(function ($) {
	var item_width = 800;
	var Interval_time = 2500;
	var animate_time = 500;
	var animate_play_time = 800;
	var lock = false;

	var play = function () {
		slide_next(animate_play_time);
	}
	var slide_next = function (_time) {
		if (lock) {
			return;
		}
		lock = true;
		var lock_t = true;
		var current = $('#switchable-content .active');
		var index = 0;
		$('#switchable-content li:not(.active)').css({
			'left': item_width + 'px'
		});
		if (current.next().length > 0) {
			index = current.next().index();
			current.animate({
				left: '-' + item_width
			}, _time, 'swing',function () {
				lock_t = !lock_t;
				if (lock_t) {
					lock = !lock;
				}
			}).removeClass('active').next().animate({
					left: '0'
				}, _time, 'swing',function () {
					lock_t = !lock_t;
					if (lock_t) {
						lock = !lock;
					}
				}).addClass('active');
		}
		else {
			current.animate({
				left: '-' + item_width
			}, _time, 'swing',function () {
				lock_t = !lock_t;
				if (lock_t) {
					lock = !lock;
				}
			}).removeClass('active');
			$('#switchable-content .item').first().animate({
				left: '0'
			}, _time, 'swing',function () {
				lock_t = !lock_t;
				if (lock_t) {
					lock = !lock;
				}
			}).addClass('active');
		}
		$('#switchable-nav .trans_cover').removeClass('active');
		$('#switchable-nav .trans_cover:eq(' + index + ')').addClass('active');
	};

	var slide_prev = function (_time) {
		if (lock) {
			return;
		}
		lock = true;
		var lock_t = true;
		var current = $('#switchable-content .active');
		var index = 0;
		$('#switchable-content li:not(.active)').css({
			'left': '-' + item_width + 'px'
		});
		if (current.prev().length > 0) {
			index = current.prev().index();
			current.animate({
				left: item_width
			}, _time, 'swing',function () {
				lock_t = !lock_t;
				if (lock_t) {
					lock = !lock;
				}
			}).removeClass('active').prev().animate({
					left: '0'
				}, _time, 'swing',function () {
					lock_t = !lock_t;
					if (lock_t) {
						lock = !lock;
					}
				}).addClass('active');
		}
		else {
			index = $('#switchable-nav .trans_cover').length - 1;
			current.animate({
				left: item_width
			}, _time, 'swing',function () {
				lock_t = !lock_t;
				if (lock_t) {
					lock = !lock;
				}
			}).removeClass('active');
			$('#switchable-content .item').last().animate({
				left: '0'
			}, _time, 'swing',function () {
				lock_t = !lock_t;
				if (lock_t) {
					lock = !lock;
				}
			}).addClass('active');
		}
		$('#switchable-nav .trans_cover').removeClass('active');
		$('#switchable-nav .trans_cover:eq(' + index + ')').addClass('active');
	};

	var slide_show = function (index, _time) {
		if (lock) {
			return;
		}
		lock = true;
		var lock_t = true;
		var current = $('#switchable-content .active');
		if (current.index() == index) {
			lock = false;
			return;
		}
		$('#switchable-content li:not(.active)').css({
			'left': item_width + 'px'
		});
		current.animate({
			left: '-' + item_width
		}, _time, 'swing',function () {
			lock_t = !lock_t;
			if (lock_t) {
				lock = !lock;
			}
		}).removeClass('active');
		$('#switchable-content .item:eq(' + index + ')').animate({
			left: '0'
		}, _time, 'swing',function () {
			lock_t = !lock_t;
			if (lock_t) {
				lock = !lock;
			}
		}).addClass('active');
		$('#switchable-nav .trans_cover').removeClass('active');
		$('#switchable-nav .trans_cover:eq(' + index + ')').addClass('active');
	};

	var MyInterval = setInterval(play, Interval_time);
	$('#prev').click(function () {
		slide_prev(animate_time);
		return false;
	});

	$('#next').click(function () {
		slide_next(animate_time);
		return false;
	});
	$('#slider').mouseenter(function () {
		clearInterval(MyInterval);
	});
	$('#slider').mouseleave(function () {
		MyInterval = setInterval(play, Interval_time);
	});
	$('#switchable-nav li').click(function () {
		slide_show($(this).index(), animate_time);
		return false;
	});
});